package com.jintoufs.web.action.scene;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.QueryRule;
import com.jintoufs.dao.scene.SceneOrderDetailMapper;
import com.jintoufs.domain.account.AccountDetail;
import com.jintoufs.domain.constants.Constants;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.domain.scene.SceneOrder;
import com.jintoufs.domain.scene.SceneOrderDetail;
import com.jintoufs.domain.shop.Shop;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.account.AccountDetailService;
import com.jintoufs.service.customer.CustomerService;
import com.jintoufs.service.divided.DividedService;
import com.jintoufs.service.scene.SceneOrderService;
import com.jintoufs.service.scene.SceneService;
import com.jintoufs.service.shop.ShopService;
import com.jintoufs.util.date.DateUtils;
import com.jintoufs.web.action.common.JxlsHandler;
import com.jintoufs.web.action.refund.RefundAction;
import com.jintoufs.web.action.shiro.bind.annotation.CurrentUser;
import com.jintoufs.web.action.support.JqgridPage;
import com.jintoufs.web.action.support.SupportAction;
import org.apache.commons.lang.StringUtils;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.dozer.loader.xml.ELEngine;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.name;

@Controller
@RequestMapping("scene/order")
public class SceneOrderAction extends SupportAction {

    @Resource
    private DividedService dividedService;
    @Resource
    private SceneOrderService sceneOrderService;
    @Resource
    private RefundAction refundAction;
    @Resource
    private ShopService shopService;
    @Resource
    private CustomerService customerService;
    @Resource
    private AccountDetailService accountDetailService;
    @Resource
    private SceneOrderDetailMapper sceneOrderDetailMapper;

    @Resource
    private SceneService sceneService;

    @RequestMapping("list")
    public String list(Model model,String code ,String name,String startDate,String endDate,String statusVal) {
        model.addAttribute("code",code);
        model.addAttribute("name",name);
        model.addAttribute("startDate",startDate);
        model.addAttribute("endDate",endDate);
        model.addAttribute("statusVal",statusVal);
        return "scene/order/list";
    }

    @RequestMapping("query")
    @ResponseBody
    @SystemControllerLog(description = "获取鱼·定制订单列表")
    public JqgridPage query(HttpServletRequest request,int page,
                            int rows, String sidx,
                            String sord,
                            String filters, String code , String name, String startDate, String endDate, String statusVal, @CurrentUser Customer customer) {
        PageHelper.startPage(page, rows);
        QueryFilters filters_;
        String status=request.getParameter("statusVal");
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }

        if(StringUtils.isNotEmpty(code)){
            QueryRule role = new QueryRule();
            role.setField("so.id");
            role.setOp("cn");
            role.setData(code);
            filters_.getRules().add(role);
        }
        if(StringUtils.isNotEmpty(name)){
            QueryRule role = new QueryRule();
            role.setField("cust.mobile_phone");
            role.setOp("cn");
            role.setData(name);
            filters_.getRules().add(role);
        }
        if(StringUtils.isNotEmpty(statusVal)){
            QueryRule role = new QueryRule();
            role.setField("so.status");
            role.setOp("eq");
            role.setData(statusVal);
            filters_.getRules().add(role);
        }
        if(StringUtils.isNotEmpty(startDate)){
            QueryRule role = new QueryRule();
            role.setField("so.create_time");
            role.setOp("ge");
            role.setData(startDate);
            filters_.getRules().add(role);
        }
        if(StringUtils.isNotEmpty(endDate)){
            QueryRule role = new QueryRule();
            role.setField("so.create_time");
            role.setOp("le");
            role.setData(endDate);
            filters_.getRules().add(role);
        }
        
        filters_.setSidx(sidx);
        filters_.setSord(sord);
        QueryRule role = new QueryRule();
        role.setField("spm.customer_id");
        role.setOp("eq");
        role.setData(customer.getId());
        filters_.getRules().add(role);

        List<Map<String,Object>> hotels = sceneOrderService.selectByFilter(filters_);
        PageInfo pageInfo = new PageInfo(hotels);
        return new JqgridPage(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(),
                pageInfo.getList());
    }

    @RequestMapping("download")
    @ResponseBody
    @SystemControllerLog(description = "下载鱼·定制订单列表")
    public void download(HttpServletRequest request, HttpServletResponse response, String sidx,
                               String sord,
                               String filters, String code , String name, String startDate, String endDate,
                               String statusVal, @CurrentUser Customer customer) {

        QueryFilters filters_;
        PageHelper.startPage(1, 10000);
        String status=request.getParameter("statusVal");
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }

        if(StringUtils.isNotEmpty(code)){
            QueryRule role = new QueryRule();
            role.setField("so.id");
            role.setOp("cn");
            role.setData(code);
            filters_.getRules().add(role);
        }
        if(StringUtils.isNotEmpty(name)){
            QueryRule role = new QueryRule();
            role.setField("cust.mobile_phone");
            role.setOp("cn");
            role.setData(name);
            filters_.getRules().add(role);
        }
        if(StringUtils.isNotEmpty(statusVal)){
            QueryRule role = new QueryRule();
            role.setField("so.status");
            role.setOp("eq");
            role.setData(statusVal);
            filters_.getRules().add(role);
        }
        if(StringUtils.isNotEmpty(startDate)){
            QueryRule role = new QueryRule();
            role.setField("so.create_time");
            role.setOp("ge");
            role.setData(startDate);
            filters_.getRules().add(role);
        }
        if(StringUtils.isNotEmpty(endDate)){
            QueryRule role = new QueryRule();
            role.setField("so.create_time");
            role.setOp("le");
            role.setData(endDate);
            filters_.getRules().add(role);
        }

        filters_.setSidx(sidx);
        filters_.setSord(sord);
        QueryRule role = new QueryRule();
        role.setField("spm.customer_id");
        role.setOp("eq");
        role.setData(customer.getId());
        filters_.getRules().add(role);

        List<Map<String,Object>> r = sceneOrderService.selectByFilter(filters_);
        try {
            JxlsHandler.report(request,response,"download.xls",r,"download.xls");
        }catch (IOException e){

        }
    }


    @RequestMapping("confirm")
    @ResponseBody
    public AjaxMsg confirm(String id,@CurrentUser Customer customer){
        AjaxMsg msg = new AjaxMsg();
        try {
            sceneOrderService.payConfirm(id);

            msg.setCode("200");
            msg.setMessage("成功");
        } catch (Exception e) {
            e.printStackTrace();
            msg.setCode(AjaxMsg.FAILURE);
        }

        return msg;
    }

    @RequestMapping(value="/detail")
    public String detail(String id, Model model) {
        Map<String,Object> param = new HashMap<>();
        SceneOrderDetail sceneOrderDetail = sceneOrderDetailMapper.selectBySceneOrderId(id).get(0);
        SceneOrder sceneOrder = sceneOrderService.getById(id);
        Map<String, Object> scene = sceneService.getById(sceneOrder.getSceneId());

        model.addAttribute("orderDetail", sceneOrderDetail);
        model.addAttribute("sceneOrder", sceneOrder);
        model.addAttribute("scene", scene);
        return "scene/order/detail";
    }
//    @RequestMapping("refund")
//    @ResponseBody
//    public AjaxMsg refund(String id){
//        AjaxMsg msg = new AjaxMsg();
//        msg.setCode(AjaxMsg.SUCCESS);
//        msg.setMessage("成功");
//        try {
//            hotelOrderService.refund(id);
//        } catch (Exception e) {
//            msg.setCode(AjaxMsg.FAILURE);
//            msg.setMessage(e.getMessage());
//        }
//        return msg;
//    }


    @RequestMapping("verify")
    @ResponseBody
    public AjaxMsg verify(String id){
        AjaxMsg ajaxMsg = new AjaxMsg();
        SceneOrder sceneOrder = sceneOrderService.getById(id);
        if (DateUtils.intervalDays(sceneOrder.getModifyTime(),new Date())>=1&&sceneOrder.getStatus().equals(Constants.ORDER_STATUS_CONFIRM)) {
            dividedService.doDivided(sceneOrder,getUUID(),"scene");
            }else {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("当前订单状态不可核销，请检查订单已确认或订单处于确认状态已经一天！");
        }

        return ajaxMsg;



    }
}
